/* eslint-disable */
var FID_getContainer = 1;
var FID_getCSPCount = 2;
var FID_getStatusCode = 3;
var FID_genKeyPair = 4;
var FID_addEntry = 5;
var FID_putEntry = 6;
var FID_getProvider = 7;
var FID_putContainer = 8;
var FID_putProvider = 9;
var FID_enumCSPName = 10;
var FID_putCert = 11;
var FID_reset = 12;
var FID_sign = 13;
var FID_getStatusString = 14;
var FID_SignData = 15;
var FID_putSerialNumber = 16;
var FID_OpenReader = 17;
var FID_CardReset = 18;
var FID_CardApdu = 19;
var FID_CloseReader = 20;
var FID_WaitCard = 21;
var FID_WaitCardForIE = 22;
var FID_GetCert = 23;
var IsMSICBrower = false;
var objToken = null;

function SOAPClient() {
}

SOAPClient.TransData = function (nFuncID, szParam) {
  var url = "https://127.0.0.1:5000/Service";
  var protocolStr = document.location.protocol;
  /*if (protocolStr != "https:") {
    url = "http://127.0.0.1:5000/Service";
  }

  if (getChormVersion()) {
    var version = getChormVersion();
    if (version > 50) {
      url = "http://127.0.0.1:5000/Service";
    }
  }*/
  var xmlHttp = new XMLHttpRequest();
  var sr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
    "<soap:Envelope " +
    "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
    "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
    "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
    "<soap:Body>" +
    "<TransData xmlns=\"http://usbkey.nja.com\">" +
    "<nFuncID>" + nFuncID + "</nFuncID><szParam>" + szParam + "</szParam>" +
    "</TransData></soap:Body></soap:Envelope>";
  xmlHttp.open("POST", url, false);
  var soapaction = "https://usbkey.nja.com/TransData";
  xmlHttp.setRequestHeader("SOAPAction", soapaction);
  xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
  xmlHttp.send(sr);
  var nd = xmlHttp.responseXML.getElementsByTagName("ns1:TransDataReturn");
  if (nd[0] == null) {
    var resp = xmlHttp.responseText;
    var start = resp.indexOf('<ns1:TransDataReturn>');
    var end = resp.indexOf('</ns1:TransDataReturn>');
    var result = resp.substring(start + 21, end);
    return (result != null) ? result + "" : "";
  }
  var result = nd[0].childNodes[0];
  return (result != null) ? result.data + "" : "";
}
SOAPClient.TransEvent = function (nFuncID, callback) {
  var szParam = "";
  var url = "https://127.0.0.1:5000/Service";
  var protocolStr = document.location.protocol;
  if (protocolStr != "https:") {
    url = "http://127.0.0.1:5000/Service";
  }

  if (getChormVersion()) {
    var version = getChormVersion();
    if (version > 50) {
      url = "http://127.0.0.1:5000/Service";
    }
  }
  var xmlHttp = new XMLHttpRequest();
  var sr = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
    "<soap:Envelope " +
    "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
    "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
    "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
    "<soap:Body>" +
    "<TransData xmlns=\"http://usbkey.nja.com\">" +
    "<nFuncID>" + nFuncID + "</nFuncID><szParam>" + szParam + "</szParam>" +
    "</TransData></soap:Body></soap:Envelope>";
  xmlHttp.open("POST", url, true);
  var soapaction = "http://usbkey.nja.com/TransData";
  xmlHttp.setRequestHeader("SOAPAction", soapaction);
  xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
  xmlHttp.onreadystatechange = function () {
    if (xmlHttp.responseXML != null) {
      var nd = xmlHttp.responseXML.getElementsByTagName("ns1:TransDataReturn");
      if (nd[0] == null) {
        var resp = xmlHttp.responseText;
        var start = resp.indexOf('<ns1:TransDataReturn>');
        var end = resp.indexOf('</ns1:TransDataReturn>');
        var result = resp.substring(start + 21, end);
        callback((result != null) ? result + "" : "");
      } else {
        var result = nd[0].childNodes[0];
        callback((result != null) ? result.data + "" : "");
      }
    }
  }
  xmlHttp.send(sr);
}
SOAPClient.init = function () {
  if (window.ActiveXObject) {
    if (objToken == null) {
      objToken = new ActiveXObject('NjaXenRoll.TokenService.1');
    }
    IsMSICBrower = true;
  } else {
    IsMSICBrower = false;
  }
}
SOAPClient.getContainer = function () {
  SOAPClient.init();
  if (IsMSICBrower) {
    return objToken.TransData(FID_getContainer, "");
  }
  return SOAPClient.TransData(FID_getContainer, "");
}
SOAPClient.getCSPCount = function () {
  SOAPClient.init();
  if (IsMSICBrower) {
    return objToken.TransData(FID_getCSPCount, "");
  }
  return SOAPClient.TransData(FID_getCSPCount, "");
}
SOAPClient.getStatusCode = function () {
  SOAPClient.init();
  if (IsMSICBrower) {
    return objToken.TransData(FID_getStatusCode, "");
  }
  return SOAPClient.TransData(FID_getStatusCode, "");
}
SOAPClient.genKeyPair = function (bits) {
  SOAPClient.init();
  if (IsMSICBrower) {
    return objToken.TransData(FID_genKeyPair, bits);
  }
  return SOAPClient.TransData(FID_genKeyPair, bits);
}
SOAPClient.addEntry = function (szName, szValue) {
  SOAPClient.init();
  if (IsMSICBrower) {
    return objToken.TransData(FID_addEntry, szName + ',' + szValue + 'A)))');
  }
  return SOAPClient.TransData(FID_addEntry, szName + ',' + szValue + 'A)))');
}
SOAPClient.putEntry = function (info) {
  SOAPClient.init();
  if (IsMSICBrower) {
    return objToken.TransData(FID_putEntry, info);
  }
  return SOAPClient.TransData(FID_putEntry, info);
}
SOAPClient.getProvider = function () {
  SOAPClient.init();
  if (IsMSICBrower) {
    return objToken.TransData(FID_getProvider, "");
  }
  return SOAPClient.TransData(FID_getProvider, "");
}
SOAPClient.putContainer = function (pVal) {
  SOAPClient.init();
  if (IsMSICBrower) {
    return objToken.TransData(FID_putContainer, pVal);
  }
  return SOAPClient.TransData(FID_putContainer, pVal);
}
SOAPClient.putProvider = function (newVal) {
  SOAPClient.init();
  if (IsMSICBrower) {
    return objToken.TransData(FID_putProvider, newVal);
  }
  return SOAPClient.TransData(FID_putProvider, newVal);
}
SOAPClient.enumCSPName = function (dwIndex, dwFlag) {
  SOAPClient.init();
  if (IsMSICBrower) {
    return objToken.TransData(FID_enumCSPName, dwIndex + ',' + dwFlag);
  }
  return SOAPClient.TransData(FID_enumCSPName, dwIndex + ',' + dwFlag);
}
SOAPClient.putCert = function (szCert) {
  SOAPClient.init();
  if (IsMSICBrower) {
    return objToken.TransData(FID_putCert, szCert);
  }
  return SOAPClient.TransData(FID_putCert, szCert);
}
SOAPClient.reset = function () {
  SOAPClient.init();
  if (IsMSICBrower) {
    return objToken.TransData(FID_reset, "");
  }
  return SOAPClient.TransData(FID_reset, "");
}
SOAPClient.sign = function () {
  SOAPClient.init();
  if (IsMSICBrower) {
    return objToken.TransData(FID_sign, "");
  }
  return SOAPClient.TransData(FID_sign, "");
}
SOAPClient.getStatusString = function () {
  SOAPClient.init();
  if (IsMSICBrower) {
    return objToken.TransData(FID_getStatusString, "");
  }
  return SOAPClient.TransData(FID_getStatusString, "");
}
SOAPClient.SignData = function (newValue) {
  SOAPClient.init();
  if (IsMSICBrower) {
    return objToken.TransData(FID_SignData, newValue);
  }
  return SOAPClient.TransData(FID_SignData, newValue);
}

SOAPClient.putSerialNumber = function (sno) {
  SOAPClient.init();
  if (IsMSICBrower) {
    return objToken.TransData(FID_putSerialNumber, sno);
  }
  return SOAPClient.TransData(FID_putSerialNumber, sno);
}
SOAPClient.OpenReader = function (szReaderName) {
  SOAPClient.init();
  if (IsMSICBrower) {
    return objToken.TransData(FID_OpenReader, szReaderName);
  }
  return SOAPClient.TransData(FID_OpenReader, szReaderName);
}
SOAPClient.CardReset = function (hReader) {
  SOAPClient.init();
  if (IsMSICBrower) {
    return objToken.TransData(FID_CardReset, hReader);
  }
  return SOAPClient.TransData(FID_CardReset, hReader);
}
SOAPClient.CardApdu = function (hReader, strCmd) {
  SOAPClient.init();
  if (IsMSICBrower) {
    return objToken.TransData(FID_CardApdu, hReader + ',' + strCmd);
  }
  return SOAPClient.TransData(FID_CardApdu, hReader + ',' + strCmd);
}
SOAPClient.CloseReader = function (hReader) {
  SOAPClient.init();
  if (IsMSICBrower) {
    return objToken.TransData(FID_CloseReader, hReader);
  }
  return SOAPClient.TransData(FID_CloseReader, hReader);
}
SOAPClient.GetCert = function (strIssuer) {
  SOAPClient.init();
  if (IsMSICBrower) {
    return objToken.TransData(FID_GetCert, strIssuer);
  }
  return SOAPClient.TransData(FID_GetCert, strIssuer);
}
SOAPClient.WatiCard = function (callback) {
  SOAPClient.init();
  if (IsMSICBrower) {
    objToken.OnWaitCard = callback;
    objToken.TransData(FID_WaitCardForIE, "");
  } else {
    SOAPClient.TransEvent(FID_WaitCard, callback);
  }
}
SOAPClient._utf8_encode = function (string) {
  string = string.replace(/\r\n/g, "\n");
  var utftext = "";
  for (var n = 0; n < string.length; n++) {
    var c = string.charCodeAt(n);
    if (c < 128) {
      utftext += String.fromCharCode(c);
    } else if ((c > 127) && (c < 2048)) {
      utftext += String.fromCharCode((c >> 6) | 192);
      utftext += String.fromCharCode((c & 63) | 128);
    } else {
      utftext += String.fromCharCode((c >> 12) | 224);
      utftext += String.fromCharCode(((c >> 6) & 63) | 128);
      utftext += String.fromCharCode((c & 63) | 128);
    }
  }
  return utftext;
}
// private method for UTF-8 decoding
SOAPClient._utf8_decode = function (utftext) {
  var string = '';
  var i = 0;
  var c = c1 = c2 = 0;
  while (i < utftext.length) {
    c = utftext.charCodeAt(i);
    if (c < 128) {
      string += String.fromCharCode(c);
      i++;
    } else if ((c > 191) && (c < 224)) {
      c2 = utftext.charCodeAt(i + 1);
      string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
      i += 2;
    } else {
      c2 = utftext.charCodeAt(i + 1);
      c3 = utftext.charCodeAt(i + 2);
      string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
      i += 3;
    }
  }
  return string;
}
SOAPClient.encode = function (input) {
  var _keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  var output = '';
  var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  var i = 0;
  input = SOAPClient._utf8_encode(input);
  while (i < input.length) {
    chr1 = input.charCodeAt(i++);
    chr2 = input.charCodeAt(i++);
    chr3 = input.charCodeAt(i++);
    enc1 = chr1 >> 2;
    enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
    enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
    enc4 = chr3 & 63;
    if (isNaN(chr2)) {
      enc3 = enc4 = 64;
    } else if (isNaN(chr3)) {
      enc4 = 64;
    }
    output = output +
      _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
      _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
  }
  return output;
}
SOAPClient.decode = function (input) {
  var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  var output = "";
  var chr1, chr2, chr3;
  var enc1, enc2, enc3, enc4;
  var i = 0;
  input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  while (i < input.length) {
    enc1 = _keyStr.indexOf(input.charAt(i++));
    enc2 = _keyStr.indexOf(input.charAt(i++));
    enc3 = _keyStr.indexOf(input.charAt(i++));
    enc4 = _keyStr.indexOf(input.charAt(i++));
    chr1 = (enc1 << 2) | (enc2 >> 4);
    chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
    chr3 = ((enc3 & 3) << 6) | enc4;
    output = output + String.fromCharCode(chr1);
    if (enc3 != 64) {
      output = output + String.fromCharCode(chr2);
    }
    if (enc4 != 64) {
      output = output + String.fromCharCode(chr3);
    }
  }
  output = SOAPClient._utf8_decode(output);
  return output;
}

function getChormVersion() {
  var arr = navigator.userAgent.split(' ');
  var chromVersion = '';
  for (var i = 0; i < arr.length; i++) {
    if (/chrom/i.test(arr[i])) {
      chromVersion = arr[i];
    }
  }
  if (chromVersion) {
    return Number(chromVersion.split('/')[1].split('.')[0]);
  } else {
    return false;
  }

}

export default SOAPClient;
